//====================================================================================================
// Includes
//====================================================================================================

#include "EnemyFall.h"

#include "Enemy.h"

//====================================================================================================
// Class Definitions
//====================================================================================================

EnemyFall::EnemyFall(Enemy* pOwner)
	: EnemyState(pOwner, "Fall")
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

EnemyFall::~EnemyFall()
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

void EnemyFall::Load(int level)
{
	// fallen, enemy 1
	if ( level == 1 )
		mSprite.Add("enemy01_jump01.png");

	// hive, enemy 2
	else if ( level == 2 )
		mSprite.Add("enemy02_jump01.png");

	// vex, enemy 3
	else if ( level == 3 )
		mSprite.Add("enemy03_jump02.png");

	// cabal, enemy 4
	else if ( level == 4 )
		mSprite.Add("enemy04_jump02.png");
}

//----------------------------------------------------------------------------------------------------

void EnemyFall::Unload()
{
	mSprite.Unload();
}

//----------------------------------------------------------------------------------------------------

void EnemyFall::Update(float deltaTime)
{
	// Update sprite
	mSprite.Update(deltaTime);

	// Update physics
	const float kSpeed = 300.0f;
	SVector2 vel(mpOwner->GetVelocity());
	if (Input_IsKeyDown(Keys::RIGHT))
	{
		vel.x = kSpeed * deltaTime;
		mpOwner->SetFacingLeft(false);
	}
	else if (Input_IsKeyDown(Keys::LEFT))
	{
		vel.x = -kSpeed * deltaTime;
		mpOwner->SetFacingLeft(true);
	}
	else
		vel.x = 0.0f;

	mpOwner->SetVelocity(vel);
}

//----------------------------------------------------------------------------------------------------

void EnemyFall::Render(const SVector2& offset)
{
	const int kTextureWidth = mSprite.GetWidth();
	const int kTextureHeight = mSprite.GetHeight();
	const SVector2 ownerPos(mpOwner->GetPosition());
	const SVector2 renderPos(ownerPos.x - (kTextureWidth * 0.5f), ownerPos.y - kTextureHeight);
	bool facingLeft = mpOwner->IsFacingLeft();
	mSprite.SetPosition(renderPos - offset);
	mSprite.SetFlipH(facingLeft);
	mSprite.Render();
}

//----------------------------------------------------------------------------------------------------

void EnemyFall::Enter()
{
	mSprite.Play(1.0f, true);
}

//----------------------------------------------------------------------------------------------------

void EnemyFall::Exit()
{
	mSprite.Stop();
	mSprite.Reset();
}

//----------------------------------------------------------------------------------------------------

SRect EnemyFall::GetBoundingBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect
	(
		pos.x - 16.0f,
		pos.y - 56.0f,
		pos.x + 16.0f,
		pos.y
	);
}